<template>
  <el-dialog
    :title="title"
    :visible.sync="is_show"
    :close-on-click-modal="false"
    class="dialog-form"
    width="485px"
    @close="resetDialog"
    append-to-body
    :show-close="false"
  >
    <el-form label-width="120px" :show-message="false">
      <el-row>
        <el-col :span="24">
        <el-form-item label="邮箱：" prop="email">
          <el-input v-model.trim="form.email" size="small" placeholder="请输入" />
        </el-form-item>
        </el-col>
        <el-col :span="24">
        <el-form-item label="验证码：" prop="code">
          <el-input v-model.trim="form.code" size="small" placeholder="请输入" />
        </el-form-item>
        </el-col>
        <el-col :span="24">
        <el-form-item label="新密码：" prop="newPassword">
          <el-input v-model.trim="form.newPassword"  show-password size="small" placeholder="请输入" />
        </el-form-item>
        </el-col>
        <el-col :span="24">
        <el-form-item label="确认新密码：" prop="rePassword">
          <el-input v-model.trim="form.rePassword" show-password size="small" placeholder="请输入" />
        </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  <div slot="footer" class="dialog-footer">
      <el-button size="small" type="primary" plain @click="is_show = false">取消</el-button>
      <el-button :loading="btn_loading" size="small" type="primary" @click="submitForm">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
export default {
  data(){
    return{
      title: '找回密码',
      is_show: false,
      btn_loading: false,
      url: null,
      form:{
        email: null,
        code: null,
        newPassword: null,
        rePassword: null,
      },
      default_form: {},
    }
  },
  created(){
    this.default_form = JSON.parse(JSON.stringify(this.form));
  },
  methods:{
    init(row){
      this.is_show = true;
    },
    resetDialog(){
      this.form = JSON.parse(JSON.stringify(this.default_form));
    },
    verifyForm(){
      if(!this.$verify.email(this.form.email,'请输入邮箱')){
        return false;
      } 
      if(!this.$verify.null(this.form.code,'请输入验证码')){
        return false;
      }
      if(!this.$verify.len(this.form.newPassword, 6, '新密码不能少于6位')){
        return false;
      } 
      if(!this.$verify.same(this.form.newPassword, this.form.rePassword, '两次输入的密码不一致')){
        return false;
      }      
      return true;
    },
    submitForm(){
      if(!this.verifyForm()){
        return;
      }
      this.btn_loading = true;
      this.$request('post', '/admin/user/findPassword', this.form, (data,msg) => {
        this.$message.success('提交成功');
        this.is_show = false;
        this.btn_loading = false;
      }, msg => {
        this.btn_loading = false;
      })
    }
  }
}
</script>

<style>

</style>